package com.jsbs.iam.auth.service.impl;

import com.jsbs.iam.auth.dto.*;
import com.jsbs.iam.auth.mapper.UserUseMapper;
import com.jsbs.iam.auth.service.UserUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: iam
 * @BelongsPackage: com.jsbs.iam.auth.service.impl
 * @Author: WangJiYuan
 * @Description: TODO
 * @Version: 2023-01-13  16:36
 */
@Service
public class UserUseServiceImpl implements UserUseService {

    @Autowired
    private UserUseMapper userUseMapper;

    @Override
    public void deleteOrgTypeRelation(OrgTypeRelationDeleteDto deleteDto) {
        //删除组织类型和角色之间关系
        userUseMapper.deleteOrgTypeRelation(deleteDto);
        //  TODO  wjy  处理用户组与组织之间的关系
    }

    @Override
    @Transactional
    public void deleteUserRelation(UserInfoDeleteDto deleteDto) {
        //清除用户与角色之间的关系
        userUseMapper.deleteUserRoleRelation(deleteDto);
        //清除用户与用户组之间的关系
        userUseMapper.deleteUserGroupRelation(deleteDto);
    }

    @Override
    @Transactional
    public void deleteApplyRelation(ApplyConfigDeleteDto deleteDto) {
        //角色
        //删除角色与应用的关联关系
        userUseMapper.deleteRoleApply(deleteDto);
        //删除角色与菜单的关联关系
        userUseMapper.deleteRoleMenuApply(deleteDto);
        //删除角色与按钮的关联关系
        userUseMapper.deleteRoleButtonApply(deleteDto);
        //用户组
        //删除用户组与菜单的关联关系
        userUseMapper.deleteUserGroupMenuApply(deleteDto);
        //删除用户组与按钮的关联关系
        userUseMapper.deleteUserGroupButtonApply(deleteDto);
    }

    @Override
    @Transactional
    public void deleteMenuRelation(MenuManagerDeleteDto deleteDto) {
        //角色
        //删除角色与菜单的关联关系
        userUseMapper.deleteRoleMenu(deleteDto);
        //删除角色与按钮的关联关系
        userUseMapper.deleteRoleButton(deleteDto);
        //用户组
        //删除用户组与菜单的关联关系
        userUseMapper.deleteUserGroupMenu(deleteDto);
        //删除用户组与按钮的关联关系
        userUseMapper.deleteUserGroupButton(deleteDto);
    }

    @Override
    @Transactional
    public void deleteButtonRelation(ButtonDeleteDto deleteDto) {
        MenuManagerDeleteDto menuDto = new MenuManagerDeleteDto();
        menuDto.setButtonCodes(deleteDto.getButtonCodeData());
        //角色
        //删除角色与按钮的关联关系
        userUseMapper.deleteRoleButton(menuDto);
        //用户组
        //删除用户组与按钮的关联关系
        userUseMapper.deleteUserGroupButton(menuDto);
    }

    @Override
    public List<String> getUserDataByApply(ApplyConfigGetDto applyConfigGetDto) {
        List<String> userData = new ArrayList<>();
        //查询应用绑定角色下的用户
        List<String> roleUserCodes = userUseMapper.getUserDataByRoleApply(applyConfigGetDto);
        userData.addAll(roleUserCodes);
        //查询应用绑定用户组的用户
        List<String> userGroupUserCodes = userUseMapper.getUserDataByUserGroupApply(applyConfigGetDto);
        userData.addAll(userGroupUserCodes);
        return userData;
    }
}
